<?php
/*****************************************************************************\
+-----------------------------------------------------------------------------+
| X-Cart                                                                      |
| Copyright (c) 2001-2007 Ruslan R. Fazliev <rrf@rrf.ru>                      |
| All rights reserved.                                                        |
+-----------------------------------------------------------------------------+
| PLEASE READ  THE FULL TEXT OF SOFTWARE LICENSE AGREEMENT IN THE "COPYRIGHT" |
| FILE PROVIDED WITH THIS DISTRIBUTION. THE AGREEMENT TEXT IS ALSO AVAILABLE  |
| AT THE FOLLOWING URL: http://www.x-cart.com/license.php                     |
|                                                                             |
| THIS  AGREEMENT  EXPRESSES  THE  TERMS  AND CONDITIONS ON WHICH YOU MAY USE |
| THIS SOFTWARE   PROGRAM   AND  ASSOCIATED  DOCUMENTATION   THAT  RUSLAN  R. |
| FAZLIEV (hereinafter  referred to as "THE AUTHOR") IS FURNISHING  OR MAKING |
| AVAILABLE TO YOU WITH  THIS  AGREEMENT  (COLLECTIVELY,  THE  "SOFTWARE").   |
| PLEASE   REVIEW   THE  TERMS  AND   CONDITIONS  OF  THIS  LICENSE AGREEMENT |
| CAREFULLY   BEFORE   INSTALLING   OR  USING  THE  SOFTWARE.  BY INSTALLING, |
| COPYING   OR   OTHERWISE   USING   THE   SOFTWARE,  YOU  AND  YOUR  COMPANY |
| (COLLECTIVELY,  "YOU")  ARE  ACCEPTING  AND AGREEING  TO  THE TERMS OF THIS |
| LICENSE   AGREEMENT.   IF  YOU    ARE  NOT  WILLING   TO  BE  BOUND BY THIS |
| AGREEMENT, DO  NOT INSTALL OR USE THE SOFTWARE.  VARIOUS   COPYRIGHTS   AND |
| OTHER   INTELLECTUAL   PROPERTY   RIGHTS    PROTECT   THE   SOFTWARE.  THIS |
| AGREEMENT IS A LICENSE AGREEMENT THAT GIVES  YOU  LIMITED  RIGHTS   TO  USE |
| THE  SOFTWARE   AND  NOT  AN  AGREEMENT  FOR SALE OR FOR  TRANSFER OF TITLE.|
| THE AUTHOR RETAINS ALL RIGHTS NOT EXPRESSLY GRANTED BY THIS AGREEMENT.      |
|                                                                             |
| The Initial Developer of the Original Code is Ruslan R. Fazliev             |
| Portions created by Ruslan R. Fazliev are Copyright (C) 2001-2007           |
| Ruslan R. Fazliev. All Rights Reserved.                                     |
+-----------------------------------------------------------------------------+
\*****************************************************************************/

#
# $Id: cc_slim.php,v 1.10.2.5 2007/07/17 11:16:21 zaa Exp $
#

if (!defined('XCART_START')) { header("Location: ../"); die("Access denied"); }

x_load('http', 'xml', 'user');

$an_clientid = $module_params["param01"];
$an_siteid = $module_params["param02"];
$an_priceid = $module_params["param03"];
$an_pass = $module_params["param05"];
$an_prefix = $module_params["param04"];
$an_key = $module_params["param06"];

$avs_codes = array(
	"A" => "Address matches - Zip Code does not",
	"B" => "Street address match, Postal code in wrong format. (international issuer)",
	"C" => "Street address and postal code in wrong formats",
	"D" => "Street address and postal code match (international issuer)",
	"E" => "AVS Error",
	"G" => "Service not supported by non-US issuer",
	"I" => "Address information not verified by international issuer.",
	"M" => "Street Address and Postal code match (international issuer)",
	"N" => "No match on address or Zip Code",
	"O" => "No Response sent",
	"P" => "Postal codes match, Street address not verified due to incompatible formats.",
	"R" => "Retry - system is unavailable or timed out",
	"S" => "Service not supported by issuer",
	"U" => "Address information is unavailable",
	"W" => "9-digit Zip Code matches - address does not",
	"X" => "Exact match to address and 9-digit Zip Code",
	"Y" => "Address and 5-digit Zip Code match",
	"Z" => "5-digit zip matches - address does not"
);

$cvv_codes = array(
	"M" => "CVV Matched.",
	"N" => "CVV No Match.",
	"P" => "Not Processed.",
	"S" => "CVV is on the card, but the shopper has indicated that CVV is not present.",
	"U" => "Issuer is not VISA certified for CVV and has not provided Visa encryption keys or both."
);

$xml_req = array();
$xml_req['request']['transtype'] = "SALE";
$xml_req['request']['first_name'] = $bill_firstname;
$xml_req['request']['last_name'] = $bill_lastname;
$xml_req['request']['email'] = $userinfo["email"];
$xml_req['request']['cardnumber'] = $userinfo["card_number"];
$xml_req['request']['expmonth'] = substr($userinfo["card_expire"], 0, 2);
$xml_req['request']['expyear'] = (2000+substr($userinfo["card_expire"], 2, 2));
$xml_req['request']['cvv2'] = $userinfo["card_cvv2"];
$xml_req['request']['amount'] = $cart["total_cost"];
$xml_req['request']['clientip'] = func_get_valid_ip($REMOTE_ADDR);
$xml_req['request']['client_transref'] = $an_prefix.join("-",$secure_oid);

$areas = func_get_profile_areas(func_get_default_fields(func_is_anonymous($login) ? "H" : "C"));
if ($areas['B']) {
	$xml_req['request']['country'] = $userinfo["b_country"];
	$xml_req['request']['state'] = ($userinfo["b_country"] == "US") ? $userinfo["b_state"] : $userinfo["b_statename"];
	if (!empty($userinfo["b_address"]))
		$xml_req['request']['address'] = $userinfo["b_address"];
	if (!empty($userinfo["b_city"]))
		$xml_req['request']['city'] = $userinfo["b_city"];
	if (!empty($userinfo["b_zipcode"]))
		$xml_req['request']['zip'] = $userinfo["b_zipcode"];
}

$post = array();
$post[] = 'clientid=' . $an_clientid;
$post[] = 'siteid=' . $an_siteid;
$post[] = 'priceid=' . $an_priceid;
$post[] = 'password=' . $an_pass;
$post[] = 'product=' . 'x-cart';
$post[] = 'ver=' . $config['version'];
$post[] = 'key=' . $an_key;
$post[] = 'XMLData=' . func_hash2xml($xml_req);

list($a, $return) = func_https_request("POST", "https://stats.slimcd.com:443/wswebservices/transact.asmx/PostXML", $post);

$return = func_xml2hash($return);
$return = $return['reply'];

if ($return['response'] == 'Success') {
	$bill_output["code"] = 1;
	$bill_output["billmes"] = ' Approved;';
} else {
	$bill_output["code"] = 2;
	$bill_output["billmes"] = ' Response: '. $return['response'].'; Description: ' . $return['description'] . ";";
}

$bill_output['billmes'] .= ' AuthCode: ' . (!zerolen($return['datablock']['authcode']) ? $return['datablock']['authcode'] : 'None') . '; GateId: ' . $return['datablock']['gateid'];

if (isset($return['datablock']['avsreply']) && in_array($return['datablock']['avsreply'], array_keys($avs_codes))) {
	$bill_output['avsmes'] = $avs_codes[$return['datablock']['avsreply']];
}

if (isset($return['datablock']['cvv2reply']) && in_array($return['datablock']['cvv2reply'], array_keys($cvv_codes))) {
	$bill_output['cvvmes'] = $cvv_codes[$return['datablock']['cvv2reply']];
}

?>
